﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aga.GestionDeArchivos.Web.Helpers;
using Aga.GestionDeArchivos.Services;
using Aga.GestionDeArchivos.Web.Models.Items;
using AutoMapper;
using Aga.GestionDeArchivos.Domain.Entities;

namespace Aga.GestionDeArchivos.Web.Controllers
{
    public class ItemsController : ControllerBase
    {
        private const int PageSize = 10;

        public ItemsController()
        {
            ViewBag.Menu = MenuKey.Admin;
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.ViewItems)]
        public ActionResult Index(int? index, string buscar)
        {
            ItemsService service = new ItemsService();

            IQueryable<Item> items;

            if (!String.IsNullOrWhiteSpace(buscar))
                items = service.GetAll(buscar);
            else
                items = service.GetAll();

            IndexModel model = new IndexModel
            {
                Items = service.GetPage(items, index ?? 0, PageSize),

                Buscar = buscar
            };

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(IndexModel model)
        {
            return RedirectToAction("Index", new { index = 0, buscar = model.Buscar });
        }


        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.CreateItems)]
        public ActionResult Create()
        {
            CreateModel model = new CreateModel();

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.CreateItems)]
        public ActionResult Create(CreateModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ItemsService service = new ItemsService();

                    Mapper.CreateMap<CreateModel, Item>()
                        .ForMember(m => m.Id, a => a.Ignore());

                    var item = Mapper.Map<CreateModel, Item>(model);

                    service.Save(item);

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditItems)]
        public ActionResult Edit(int id)
        {
            ItemsService service = new ItemsService();

            Item item = service.GetById(id);

            Mapper.CreateMap<Item, EditModel>();

            var model = Mapper.Map<Item, EditModel>(item);

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.EditItems)]
        public ActionResult Edit(EditModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ItemsService service = new ItemsService();

                    Item item = service.GetById(model.Id);

                    Mapper.CreateMap<EditModel, Item>();

                    item = Mapper.Map<EditModel, Item>(model, item);

                    service.Save(item);

                    return RedirectToAction("Index", new { buscar = model.Detalle });
                }
                else
                {
                    return View(model);
                }
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }

        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.DeleteItems)]
        public ActionResult Delete(int id)
        {
            ItemsService service = new ItemsService();

            Item item = service.GetById(id);

            DeleteModel model = new DeleteModel
            {
                Id = item.Id,
                Detalle = item.Detalle
            };

            return View(model);
        }

        [HttpPost]
        [CustomAuthorize(Actions = Aga.GestionDeArchivos.Services.Actions.DeleteItems)]
        public ActionResult Delete(DeleteModel model)
        {
            try
            {
                ItemsService service = new ItemsService();

                service.Delete(model.Id);

                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                return ShowError(ex);
            }
        }
    }
}
